/**
 * 
 */
package com.wixc.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wixc.entity.NewBean;
import com.wixc.service.NewService;
import com.wixc.util.ConstantsUtil;
import com.wixc.util.LoginUtil;
import com.wixc.util.MyStringUtils;

/**
 * @author weichuanxi
 * @date 2016年4月12日
 */
@Controller
public class NewController {
	private static Logger log = Logger.getLogger(NewController.class);
	@Autowired
	NewService newServiceImpl;

	/**
	 * zhuye
	 */
	@RequestMapping(value = "/hotnew.html")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		List<NewBean> top = new ArrayList<NewBean>();
		List<NewBean> top0 = new ArrayList<NewBean>();
		List<NewBean> top1= new ArrayList<NewBean>();
		List<NewBean> top2 = new ArrayList<NewBean>();
		List<NewBean> top3 = new ArrayList<NewBean>();
		List<NewBean> top4 = new ArrayList<NewBean>();
		List<NewBean> top5 = new ArrayList<NewBean>();
		List<NewBean> top6 = new ArrayList<NewBean>();
		List<NewBean> top7 = new ArrayList<NewBean>();
		String string = new Date(System.currentTimeMillis()) + "";
		top = this.newServiceImpl.selectByDate(string);
		for (NewBean newBean : top) {
			if (newBean.getTypeId()==0) {
				top0.add(newBean);
			}else if (newBean.getTypeId()==1) {
				top1.add(newBean);
			}else if (newBean.getTypeId()==2) {
				top2.add(newBean);
			}else if (newBean.getTypeId()==3) {
				top3.add(newBean);
			}else if (newBean.getTypeId()==4) {
				top4.add(newBean);
			}else if (newBean.getTypeId()==5) {
				top5.add(newBean);
			}else if (newBean.getTypeId()==6) {
				top6.add(newBean);
			}else if (newBean.getTypeId()==7) {
				top7.add(newBean);
			}
		}
		
		List<NewBean> circle = new ArrayList<NewBean>();
		circle = this.newServiceImpl.selectCircle();
		System.out.println(circle);
		log.info("top>>>" + top.size());
		log.info("circle>>>" + circle.size());
		mv.addObject("newbean", top);
		mv.addObject("top1", top1);
		mv.addObject("top2", top2);
		mv.addObject("top3", top3);
		mv.addObject("top4", top4);
		mv.addObject("top5", top5);
		mv.addObject("top6", top6);
		mv.addObject("top7", top7);
		mv.addObject("circlebean", circle);
		mv.setViewName("view/new");
		return mv;
	}

	/**
	 * select detail
	 */
	@RequestMapping(value = "/new-detail/{id}")
	public ModelAndView getBeanById(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		List<NewBean> newbean = this.newServiceImpl.selectById(Integer.parseInt(id.replaceAll("[^\\d]", "")));
		log.info("new-detail" + newbean.size());
		mv.addObject("newbean", newbean);
		mv.setViewName("view/newdetail");
		return mv;
	}

	/**
	 * getNewsByTypeId_Page
	 */
	@RequestMapping(value = "/new/{typeId}/{page}")
	public ModelAndView getNewsByTypeId_Page(@PathVariable String typeId, @PathVariable String page, HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		List<NewBean> newbean = this.newServiceImpl.selectByTypeId_Page(Integer.parseInt(typeId.replaceAll("[^\\d]", "")),
				Integer.parseInt(page.replaceAll("[^\\d]", "")) * 10);

		List<NewBean> newbean2 = new ArrayList<NewBean>();
		for (NewBean newBean22 : newbean) {
			String detail = newBean22.getDetail();
			Document doc = Jsoup.parse(detail);
			detail = doc.text();
			newBean22.setDetail(detail);
			newbean2.add(newBean22);
		}

		mv.addObject("newbean", newbean2);
		mv.addObject("page", Integer.parseInt(page.replaceAll("[^\\d]", "")) + 1);
		mv.addObject("typeid", Integer.parseInt(typeId.replaceAll("[^\\d]", "")));
		mv.setViewName("view/news");
		return mv;
	}

	/**
	 * ajax weather
	 */
	@RequestMapping(value = "/tianqi", method = RequestMethod.POST)
	public @ResponseBody String getBeanById(LoginUtil util, HttpServletRequest request, HttpServletResponse response) {
		String apikey = "43abbe1cf7f9842d0dd2919aa793f036";
		String result = ConstantsUtil.request(apikey).replace("HeWeather data service 3.0", "data");
		return result;
	}

	@RequestMapping(value = "/newDetail", method = RequestMethod.POST)
	public @ResponseBody NewBean newDetail(String id, HttpServletRequest request, HttpServletResponse response) {
		List<NewBean> newbean = this.newServiceImpl.selectById(Integer.parseInt(id.replaceAll("[^\\d]", "")));
		return newbean.get(0);
	}

	/**
	 * ajax more new
	 */
	@RequestMapping(value = "/more_news", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> getMoreNew(int typeid, int page, HttpServletRequest request, HttpServletResponse response) {
		List<NewBean> lists = this.newServiceImpl.selectByTypeIdAjax(page * 15, typeid);
		log.info("ajax new size>>>" + lists.size());
		List<NewBean> newbean2 = new ArrayList<NewBean>();
		for (NewBean newBean22 : lists) {
			String detail = newBean22.getDetail();
			Document doc = Jsoup.parse(detail);
			detail = doc.text();
			newBean22.setDetail(detail);
			newbean2.add(newBean22);
		}

		if (lists.size() > 0) {
			Map<String, Object> map = new LinkedHashMap<String, Object>();
			map.put("nextpage", page + 1);
			map.put("data", newbean2);
			return map;
		} else {
			return null;
		}
	}

	/**
	 * delete new
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/deleteNew", method = RequestMethod.POST)
	public @ResponseBody void deleteNew(String selectedIDs, HttpServletRequest request, HttpServletResponse response) {
		log.info("deleteNew:" + selectedIDs);
		String[] ids = selectedIDs.split(",");
		List ls = Arrays.asList(ids);
		for (Object object : ls) {
			this.newServiceImpl.batchDelete(Integer.parseInt(object + ""));
		}
	}

	/**
	 * setHotNew
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/setHotNew", method = RequestMethod.POST)
	public @ResponseBody void setHotNew(String selectedIDs, HttpServletRequest request, HttpServletResponse response) {
		log.info("selectedIDs:" + selectedIDs);
		String[] ids = selectedIDs.split(",");
		List ls = Arrays.asList(ids);
		for (Object object : ls) {
			this.newServiceImpl.setHotNew(Integer.parseInt(object + ""));
		}
	}

	/**
	 * manage new select
	 */
	@RequestMapping(value = "/selectNew", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> selectNew(int typeid, int page, long sdate, long edate, HttpServletRequest request,
			HttpServletResponse response) {
		log.info("typeid:" + typeid + "\tpage:" + page + "\tsdate:" + sdate + "\tedate:" + edate);

		List<NewBean> lists = new ArrayList<NewBean>();
		if (typeid == -1 && sdate == 0 && edate == 0) {// no date&type
			lists = this.newServiceImpl.selectByPage(page * 20);
		} else if (typeid != -1 && sdate == 0 && edate == 0) {// no date
			lists = this.newServiceImpl.selectByTypeId_Page(typeid, page * 20);
		} else {
			Date start_date = new Date(sdate);
			Date end_date = new Date(edate);
			lists = this.newServiceImpl.selectByTypeId_sdate_edate(typeid, page * 20, MyStringUtils.DateToStr(start_date, "yyyy-MM-dd"),
					MyStringUtils.DateToStr(end_date, "yyyy-MM-dd"));
		}

		int total = lists.size();
		log.info("lists:" + lists.size());
		if (lists.size() > 0) {
			Map<String, Object> map = new LinkedHashMap<String, Object>();
			map.put("nextpage", page + 1);
			map.put("total", total);
			map.put("data", lists);
			return map;
		} else {
			return null;
		}
	}

	/**
	 * manage new
	 */
	@RequestMapping(value = "/newList/{typeid}/{page}/{sdate}/{edate}")
	public ModelAndView newList(@PathVariable int typeid, @PathVariable int page, @PathVariable long sdate, @PathVariable long edate,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		String start_date = "";
		String end_date = "";
		if (start_date.length() < 2) {
			start_date = MyStringUtils.DateToStr(MyStringUtils.getDateByDayIndex(new java.util.Date(), -10), "yyyy-MM-dd HH:mm:ss");
			end_date = new Date(System.currentTimeMillis()) + "";
		} else {
			start_date = new Date(sdate) + "";
			end_date = new Date(edate) + "";
		}
		List<NewBean> lists = this.newServiceImpl.selectByTypeId_sdate_edate(typeid, page, start_date, end_date + "");
		int total = lists.size();
		log.info("sdate--edate:" + lists.size());
		List<NewBean> newbean = new ArrayList<NewBean>();
		List<NewBean> newbean2 = new ArrayList<NewBean>();
		if (page < (lists.size() / 20)) {
			newbean = lists.subList(page * 20, (page + 1) * 20);
			for (NewBean newBean22 : newbean) {
				String detail = newBean22.getDetail();
				Document doc = Jsoup.parse(detail);

				if (detail.length() > 30) {
					detail = doc.text().substring(0, 29) + "...";
				} else {
					detail = doc.text();
				}
				newBean22.setDetail(detail);
				newbean2.add(newBean22);
			}
		} else {
			newbean = null;
			newbean2 = null;
		}
		log.info("newbean2>>>" + newbean2.size());
		if (lists.size() > 0) {
			mv.addObject("nextpage", page);
			mv.addObject("total", total);
			System.out.println(newbean2);
			mv.addObject("mdata", newbean2);
			mv.setViewName("manage/newList");
			return mv;
		} else {
			return null;
		}
	}

}
